﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3.classes;
using Evaluation3.utils;

public partial class admin_company : ExPage
{
    private bool isAddNew_ = true;
    //private string SelectedTabIndex = "0";
    SqlDataSource dsDn = new SqlDataSource();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        txt_comId.ReadOnly = true;
        if(!IsPostBack) {
             
            Session[keyUtils.SES_SELECTEDTAB] = "0";
        }
        //Session[keyUtils.SES_SELECTEDTAB] = Request.QueryString["selectedTab"];
        
        dsDn.ConnectionString = ConfigurationManager.ConnectionStrings["evaConn"].ToString();
       
    }
    public override remoteUser getRemoteUser()
    {
        return (Evaluation3.classes.remoteUser)Session[keyUtils.SES_REMOTEUSER];
    }
    protected String getSelectedTab() { return Session[keyUtils.SES_SELECTEDTAB].ToString(); }
    protected void setSelectedTab(String i) { Session[keyUtils.SES_SELECTEDTAB] = i; }
    protected void FormReset() {
        txt_comId.Text = null;
        txt_compten.Value = null;
        txt_complinhvuc.Value = null;
        Calendar1.setValue(null);
        txt_trangchu.Text = null;
        txt_diachi.Value = null;
        txt_homthu.Text = null;
        txt_dienthoai.Text = null;
        isAddNew_ = true;
        lblEmailNotify.Visible = false;

        grvCoLst.DataBind();
    }

    protected void companySelectCommand(object sender, DataGridCommandEventArgs e)
    {
         String idGuest = RegCompanyListView.DataKeys[(int)e.Item.ItemIndex].ToString();
         makhach.Value = idGuest;
         detailData.UpdateParameters.UpdateValues(Context, makhach );
         rp_detail.DataBind();
    }
    protected void btn_permiss_click(object sender, EventArgs e) {
        try
        {
            String logo = ((HiddenField)rp_detail.Controls[0].Controls[1]).Value;
            String ten = ((HiddenField)rp_detail.Controls[0].Controls[3]).Value;
            String manguoidangky = ((HiddenField)rp_detail.Controls[0].Controls[5]).Value;
            String nam = ((HiddenField)rp_detail.Controls[0].Controls[7]).Value;
            String nganhnghe = ((HiddenField)rp_detail.Controls[0].Controls[9]).Value;
            String diachi = ((HiddenField)rp_detail.Controls[0].Controls[11]).Value;
            String dienthoai = ((HiddenField)rp_detail.Controls[0].Controls[13]).Value;
            String hopthu = ((HiddenField)rp_detail.Controls[0].Controls[15]).Value;
            isAddNew_ = true;
            String namtemp = "";
            if (nam != null && nam.Trim().Length > 0)
            {
                namtemp = nam.Substring(0, 10);

            }
            //new ShowMessageBox(Response, manguoidangky);
            SaveForm(null, ten, nganhnghe, namtemp, "http://", diachi, hopthu, dienthoai);
            dataUtils.ExecuteQuery("update guest set dacapphep = 1 where idguest='" + manguoidangky + "'", CommandType.Text);
            makhach.Value = "0";
            //detailData.UpdateParameters.UpdateValues(Context, makhach);
            //rp_detail.DataBind();
            RegCompanyListView.DataBind();
        } catch  (ArgumentOutOfRangeException ex)  {
        }

    }
    protected String SaveForm(String id, String tendn, String linhvuc, String namthanhlap, String trangchu, String diachi, String homthu, String dienthoai) 
    {
            String alert = "";
            if (isAddNew_ ==true && ( id == null || id.Trim().Length == 0)) 
            {
                SqlDataSource dsCom = new SqlDataSource(ConfigurationManager.ConnectionStrings["evaConn"].ToString(), "SELECT * FROM DoanhNghiep");
                DataTable dt = ((DataView)dsCom.Select(DataSourceSelectArguments.Empty)).Table;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (homthu == dt.Rows[0]["Email"].ToString())
                    {
                        lblEmailNotify.Visible = true;
                        dt.Dispose();
                        dsCom.Dispose();
                        return "Email này đã dùng để đăng ký rồi";
                    }
                }
                
                dsDn.InsertCommand = "INSERT INTO DoanhNghiep(TenDoanhNghiep, NamThanhLap, DiaChi, SoDienThoai_Fax, Email, ChucNang, WebSite, Status) VALUES (N'" + tendn + "','" + namthanhlap + "',N'" + diachi + "','" + dienthoai + "','" + homthu + "',N'" + linhvuc + "','" + trangchu + "',1)";
                //Response.Write(dsDn.InsertCommand);
                dsDn.Insert();                
                dt.Dispose();
                dsCom.Dispose();
                alert = "Đã thêm mới Doanh nghiệp vào hệ thống chấm điểm.";
            }
            else if (id != null) { 
                //update
                dsDn.UpdateCommand = "UPDATE DoanhNghiep SET TenDoanhNghiep = N'" + tendn + "', NamThanhLap='" + namthanhlap + "', DiaChi = N'" + diachi + "', SoDienThoai_Fax='" + dienthoai + "', Email='" + homthu + "', ChucNang = N'" + linhvuc + "', WebSite = '" + trangchu + "' WHERE MaDoanhNghiep = " + id;
                dsDn.Update();

              //  Session[keyUtils.SES_SELECTEDTAB] = "0";//Quay trở về tab đầu

                alert = "Cập nhật thành công";
            }
            grvCoLst.DataBind();
           // Session[keyUtils.SES_SELECTEDTAB] = "0";//Quay trở về tab đầu
            return alert;
    
    }
    protected void ChangeTabIndex(object sender, EventArgs e) {
        String senderId = ((LinkButton)sender).ID ;
        
        setSelectedTab(senderId.Substring(senderId.Length - 1));
        
    }
    protected void grvCoLst_RowCommand(object sender, GridViewCommandEventArgs e)
    { 
        if(e.CommandName=="Sua")
        {
            SqlDataSource dsCom = new SqlDataSource(ConfigurationManager.ConnectionStrings["evaConn"].ToString(), "SELECT * FROM DoanhNghiep WHERE MaDoanhNghiep = " + (String)e.CommandArgument);
            DataTable dt = ((DataView)dsCom.Select(DataSourceSelectArguments.Empty)).Table;

            txt_comId.Text = dt.Rows[0]["MaDoanhNghiep"].ToString();
            txt_compten.Value = dt.Rows[0]["TenDoanhNghiep"].ToString();
            txt_complinhvuc.Value = dt.Rows[0]["ChucNang"].ToString();
            Calendar1.setValue(dt.Rows[0]["NamThanhLap"].ToString());
            txt_trangchu.Text = dt.Rows[0]["WebSite"].ToString();
            txt_diachi.Value = dt.Rows[0]["DiaChi"].ToString();
            txt_homthu.Text = dt.Rows[0]["Email"].ToString();
            txt_dienthoai.Text = dt.Rows[0]["SoDienThoai_Fax"].ToString();
            isAddNew_ = false;

            Session[keyUtils.SES_SELECTEDTAB] = "2";//Quay về tab edit thông tin 

            dt.Dispose();
            dsCom.Dispose();
        }
        else if(e.CommandName == "Xoa")
        {
            dsDn.DeleteCommand = "DELETE FROM DoanhNghiep WHERE MaDoanhNghiep = " + e.CommandArgument.ToString();
            dsDn.Delete();
            grvCoLst.DataBind();
        }
        
    }
    protected void grvCoLst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Thêm Comfirm Message Box vào cho nút xóa
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibDel = (ImageButton)e.Row.FindControl("delete");
            ibDel.Attributes.Add("onclick", "javascript:return confirm('Bạn muốn xóa sản phẩm " + DataBinder.Eval(e.Row.DataItem, "TenDoanhNghiep") + " ra khỏi dữ liệu ?')");
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        SaveForm(txt_comId.Text, txt_compten.Value, txt_complinhvuc.Value, Calendar1.Text, txt_trangchu.Text, txt_diachi.Value, txt_homthu.Text, txt_dienthoai.Text);
        FormReset();
    }
    protected void btn_Reset_Click(object sender, EventArgs e)
    {
        FormReset();
    }
    protected void grvCoLst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvCoLst.PageIndex = e.NewPageIndex;
        grvCoLst.DataBind();
    }
    
    protected void ShowInfo_EditCommand(object source, DataListCommandEventArgs e)
    {
        
    }
    protected void Select_Company(object sender, GridViewCommandEventArgs e)
    {

    }
    
}

